package com.operate.api.operate.meal.service.impl;

import com.operate.api.app.meal.domain.vo.AppPackageSearchVO;
import com.operate.api.app.meal.domain.vo.AppPackageVO;
import com.operate.api.base.Constants;
import com.operate.api.base.Type;
import com.operate.api.operate.meal.dao.PackageDao;
import com.operate.api.operate.meal.domain.po.PackagePO;
import com.operate.api.operate.meal.domain.vo.*;
import com.operate.api.operate.meal.service.IPackageService;
import com.operate.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
@Slf4j
public class PackageServiceImpl implements IPackageService {

    @Autowired
    private PackageDao dao;


    @Override
    public List<PackageVO> getList(PackageSearchVO searchVO) {
        return dao.getList(searchVO);
    }


    @Override
    public List<AppPackageVO> getAppList(AppPackageSearchVO searchVO) {
            return dao.getAppList(searchVO);
    }

    @Override
    public void add(PackageAddVO vo) {
        PackagePO po = new PackagePO();
        BeanUtils.copyProperties(vo, po);
        po.preInsert();
        int i = dao.insertSelective(po);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
    }

    @Override
    public void update(PackageUpdateVO vo) {
        PackagePO po = new PackagePO();
        BeanUtils.copyProperties(vo, po);
        po.preUpdate();
        int i = dao.updateByPrimaryKeySelective(po);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
    }

    @Override
    public void disablePackage(PackageStateVO vo) {
        if (StringUtils.isBlank(vo.getId())) {
            throw new BusinessException(Type.PARAM_VALIDATE_FAIL);
        }
        PackagePO packagePO = dao.selectByPrimaryKey(vo.getId());
        if (packagePO == null) {
            throw new BusinessException(Type.NOT_FOUND_ERROR.getCode(),  "用户不存在");
        }
        packagePO.setPackageState(vo.getPackageState());
        packagePO.preUpdate();
        int i = dao.updateByPrimaryKeySelective(packagePO);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
    }
    @Override
    public void delete(String id) {
        if (StringUtils.isBlank(id)) {
            throw new BusinessException(Type.PARAM_VALIDATE_FAIL);
        }
        int i = dao.deleteByPrimaryKey(id);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
    }

}